import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

class Solution1 {
    int sum = 0;
    List<Integer> list = new ArrayList<>();

    public int subsetXORSum(int[] nums) {
        dfs(nums, 0);
        return sum;
    }

    public void dfs(int[] nums, int u) {
        if (u == nums.length) {
            if (list.size() > 0) {
                int x = list.get(0);
                for (int i = 1; i < list.size(); i++) {
                    x ^= list.get(i);
                }
                sum += x;
            }
            return;
        }

        //不选
        dfs(nums, u + 1);
        //选
        list.add(nums[u]);
        dfs(nums, u + 1);
        list.remove(list.size() - 1);
    }
}

class Solution2 {
    int sum = 0;
    int x = 0;

    public int subsetXORSum(int[] nums) {
        dfs(nums, 0);
        return sum;
    }

    public void dfs(int[] nums, int u) {
        if (u == nums.length) {
            sum += x;
            return;
        }

        //不选
        dfs(nums, u + 1);
        //选
        x ^= nums[u];
        dfs(nums, u + 1);
        x ^= nums[u];
    }
}

class Solution {
    List<List<Integer>> lists = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    boolean[] st;

    public List<List<Integer>> permuteUnique(int[] nums) {
        st = new boolean[nums.length];
        Arrays.sort(nums);
        dfs(nums, 0);
        return lists;
    }

    public void dfs(int[] nums, int u) {
        if (u == nums.length) {
            lists.add(new ArrayList<>(list));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!st[i] && (i == 0 || nums[i] != nums[i - 1] || !st[i - 1])) {
                st[i] = true;
                list.add(nums[i]);
                dfs(nums, u + 1);
                st[i] = false;
                list.remove(list.size() - 1);
            }
        }
    }
}

public class Test {
    public static void main(String[] args) {
        int ret = new Solution1().subsetXORSum(new int[]{1, 2, 3});
    }
}
